package com.rzhd.coursepatriarch.common.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.support.annotation.IntDef;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * @author yql
 * @date: 2018/7/31.
 */
public class NetworkMyUtil {

    public static NetworkMyUtil instance;
    public Context mContext;

    public NetworkMyUtil(Context context) {
        this.mContext = context;
    }

    public static NetworkMyUtil getInstance(Context context) {
        if (instance == null) {
            synchronized (NetworkMyUtil.class) {
                if (instance == null) {
                    instance = new NetworkMyUtil(context);
                }
            }
        }
        return instance;
    }

    /**
     * 是否连接网络
     *
     * @return
     */
    public boolean isHaveNetWork() {
        if (getNetWorkState() == NetworkType.NETWORK_NONE) {
            //断网状态
            return false;
        }
        return true;
    }

    /**
     * 获取网络连接状态
     *
     * @return
     */
    public int getNetWorkState() {
        if (mContext == null) {
            throw new UnsupportedOperationException("please use NetUtils before init it");
        }
        // 得到连接管理器对象
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {

            ConnectivityManager connectivityManager = (ConnectivityManager) mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo activeNetworkInfo = connectivityManager
                    .getActiveNetworkInfo();
            if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
                if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
                    return NetworkType.NETWORK_WIFI;
                } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
                    return NetworkType.NETWORK_MOBILE;
                }
            } else {
                return NetworkType.NETWORK_NONE;
            }

        } else {
            //系统版本21后获取网络状态
            ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);

            //获取所有网络连接的信息
            Network[] networks = connMgr.getAllNetworks();
            //通过循环将网络信息逐个取出来
            for (int i = 0; i < networks.length; i++) {
                //获取ConnectivityManager对象对应的NetworkInfo对象
                NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);
                if (networkInfo.isConnected()) {
                    if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                        return NetworkType.NETWORK_MOBILE;
                    } else {
                        return NetworkType.NETWORK_WIFI;
                    }
                }
            }
        }
        return NetworkType.NETWORK_NONE;
    }

    @IntDef({NetworkType.NETWORK_NONE,
            NetworkType.NETWORK_MOBILE,
            NetworkType.NETWORK_WIFI
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface NetworkType {
        /**
         * 没有连接网络
         */
        int NETWORK_NONE = -1;
        /**
         * 移动网络
         */
        int NETWORK_MOBILE = 0;
        /**
         * 无线网络
         */
        int NETWORK_WIFI = 1;
    }

}
